define(function (require, exports, module) {
        exports.init = function (element, cb) {
                var basic=require('basic');
                var jsonAPI = require('json-api');
                var mustache = require('mustache');
                var id=getQueryParam('id');
                var timeInterval=null;
                var OVER_MINUTES=15;
                require('timer');

                defineModule({
                        queryDetail:function(cb)
                        {
                                var mobile=basic.getLoginUser();
                                jsonAPI.restful('/api/payment/read', {mobile:mobile,id:id}, function (err, result) {
                                        if (jsonAPI.handleErrors(err, result)) {
                                               console.log(JSON.stringify(result));
                                                var data=result.result;

                                                $("#title").text(data.parkingname);
                                                var entertime = new Date(data.entertime.replace(/-/g, "/"));
                                                $("#entertime").text(entertime.format("MM-dd HH:mm"));

                                                var paidtime=new Date();
                                                if(data.paidtime)
                                                {
                                                        //todo  for test
                                                       paidtime=new Date(data.paidtime.replace(/-/g, "/"));
                                                }
                                                $spa.createTimer(paidtime.getTime(),$("#totalTime"));

                                                var isOver=$module.isOverTime(paidtime);
                                                if (data.entertime && !data.lefttime && data.haspaid == 1) {
                                                        var paymentmethod;
                                                        switch (data.paymentmethod) {
                                                                case "alipay":
                                                                        paymentmethod = '支付宝支付';
                                                                        break;
                                                                case "wx":
                                                                        paymentmethod = '微信支付' ;
                                                                        break;
                                                                case "wx_pub":
                                                                        paymentmethod = '微信支付';
                                                                        break;
                                                        }

                                                        $(".payResult").text("已" + paymentmethod );
                                                        $("#status").attr("src", "images/left.jpg").show();
                                                }

                                                if(isOver) //超时
                                                {
                                                        $("#note1").html("您15分钟内未立场,将在离场时以<span style='color:Red'>现金方式</span>支付!")
                                                        $("#note2").show();
                                                        $("#parkingfee").text(data.parkingfee / 100);
                                                }
                                                timeInterval=setInterval(
                                                        function()
                                                        {
                                                                $module.getTime(paidtime)
                                                        },1000);
                                        }
                                        cb&&cb();
                                });
                        },
                         isOverTime:function(start) //计算支付时间与当前时间的时间差　在不在15分钟之内
                         {
                                 var data=new Date().getTime()-start.getTime()  //时间差的毫秒数
                                 var mills=15*60*1000;
                                 return data>mills;
                         },
                        getTime:function(payTime)
                        {
                                var date3 = new Date().getTime() - payTime.getTime();
                                var hours = Math.floor(date3 / (3600 * 1000))

                                //计算相差分钟数
                                var leave1 = date3 % (24 * 3600 * 1000)    //计算天数后剩余的毫秒数
                                var leave2 = leave1 % (3600 * 1000)        //计算小时数后剩余的毫秒数
                                var minutes = Math.floor(leave2 / (60 * 1000))

                                //计算相差秒数
                                var leave3 = leave2 % (60 * 1000)      //计算分钟数后剩余的毫秒数
                                var seconds = Math.round(leave3 / 1000)

                                if(minutes<OVER_MINUTES&&seconds==59)//15分钟以内,每分钟获取一次服务器数据,判断是否离开，如果离开页面自动跳转到订单记录页面
                                {
                                        console.log('1:'+minutes+':'+seconds);
                                        var mobile=basic.getLoginUser();
                                        jsonAPI.restful('/api/payment/read', {mobile:mobile,id:id}, function (err, result) {
                                                if (jsonAPI.handleErrors(err, result)) {
                                                        var  data=result.result;
                                                        if(data.lefttime){
                                                                $spa.showOk('顺利离场!');
                                                                setTimeout(function () {
                                                                        $spa.hideOk();
                                                                        window.hashTo('#orderRecord');
                                                                }, 2000);
                                                        }
                                                }
                                        });
                                }

                                if(minutes>=OVER_MINUTES&&minutes%5==0&&seconds==10) //分钟数是15 ,20,25...的时候刷新停车费用,为了避免与服务器时间有差　10秒的时候再刷新数据
                                {
                                        console.log('2:'+minutes+':'+seconds);
                                        var mobile=basic.getLoginUser();
                                        jsonAPI.restful('/api/payment/read', {mobile:mobile,id:id}, function (err, result) {
                                                if (jsonAPI.handleErrors(err, result)) {
                                                        var  data=result.result;
                                                        if(data.lefttime){
                                                                $spa.showOk('已经离场!');
                                                                setTimeout(function () {
                                                                        $spa.hideOk();
                                                                        window.hashTo('#orderRecord');
                                                                }, 2000);
                                                        }
                                                        else{
                                                                if(minutes==OVER_MINUTES)
                                                                {
                                                                        $("#note1,.payResult").html("您15分钟内未立场,将在离场时以<span style='color:Red'>现金方式</span>支付!")
                                                                        $("#note2").show();
                                                                }
                                                                $("#parkingfee").text(data.parkingfee / 100);
                                                        }
                                                }
                                        });
                                }
                        },
                        onDestroy:function()
                        {
                                $spa.destoryTimer();
                                if(timeInterval!=null) window.clearInterval(timeInterval); //退出页面时清除计时器
                        }
                })

              $module.queryDetail(function(){
                        cb && cb();
                })
        };
});